package com.example.feifei.newproject;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.example.feifei.view.ChuckWaveView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by feifei on 2017/7/18.
 */

public class MyViewActivity extends Activity implements View.OnClickListener{

    private Button btn_startani;

    private ImageView img_test;

    private ChuckWaveView mWaveView = null;
    private SeekBar mSeekBar = null;

    private float mCurrentHeight = 0;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myview);
        initView();
    }

    private void initView(){
        btn_startani = (Button) this.findViewById(R.id.btn_startani);
        btn_startani.setOnClickListener(this);
        img_test = (ImageView) this.findViewById(R.id.img_test);
        mWaveView = (ChuckWaveView) findViewById(R.id.main_wave_v);
        mSeekBar = (SeekBar) findViewById(R.id.main_sb);
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int process, boolean fromUser) {
                loadWaveData(process * 1.0f / 100);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }
    private void loadWaveData(float height) {
        final List<Animator> mAnimators = new ArrayList<Animator>();
        ObjectAnimator waveShiftAnim = ObjectAnimator.ofFloat(mWaveView, "waveShiftRatio", 0f, 1f); //水平方向循环
        waveShiftAnim.setRepeatCount(ValueAnimator.INFINITE);
        waveShiftAnim.setDuration(2000);
        waveShiftAnim.setInterpolator(new LinearInterpolator());
        mAnimators.add(waveShiftAnim);

        ObjectAnimator waterLevelAnim = ObjectAnimator.ofFloat(mWaveView, "waterLevelRatio", mCurrentHeight, height); //竖直方向从0%到x%
        waterLevelAnim.setDuration(6000);
        waterLevelAnim.setInterpolator(new DecelerateInterpolator());
        mAnimators.add(waterLevelAnim);

        mWaveView.invalidate();
        AnimatorSet mAnimatorSet = new AnimatorSet();
        mAnimatorSet.playTogether(mAnimators);
        mAnimatorSet.start();

        mCurrentHeight = height;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_startani:
                ObjectAnimator xAnimator = ObjectAnimator.ofFloat(img_test, "translationX", 0f, 500f);// 初始化动画，设置各个参数
                ObjectAnimator yAnimator =  ObjectAnimator.ofFloat(img_test,"translationY",0f,500f);
//                ObjectAnimator rotation = ObjectAnimator.ofFloat(img_test,"rotation",0f,360f);
                AnimatorSet set = new AnimatorSet();
                set.setDuration(2000);
//                1)  BounceInterpolator：弹跳效果；
//                2)  AccelerateInterpolator：逐渐加速；
//                3)  DecelerateInterpolator：逐渐减速；
//                4)  AccelerateDecelerateInterpolator：先加速后减速；
//                5)  OvershootInterpolator：到达目标点时“跑过头了”，再返回到目标点；
//                6)  AnticipateInterpolator：移动之前先向后“助跑”；
//                7)  AnticipateOvershootInterpolator：OvershootInterpolator和AnticipateInterpolator的组合效果；
//                8)  CycleInterpolator：对于指定的动画，正向做一遍，反响做一遍；
                set.setInterpolator(new AnticipateOvershootInterpolator());
                set.playTogether(xAnimator,yAnimator);
                set.start();
                break;
        }
    }
}
